//
//  HCMineHeadView.m
//  Aierle
//
//  Created by jiayibai-02por on 2017/6/3.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCMineHeadView.h"

@interface HCMineHeadView()

@property (nonatomic, strong) UIImageView *userHeadImageView;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UIButton *couponsButton;
@property (nonatomic, strong) UIButton *integralButton;

@end

@implementation HCMineHeadView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        [self initializeView:frame];
    }
    return self;
}

- (void)initializeView:(CGRect)frame {

    self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    self.backImageView.image = [UIImage imageNamed:@"ming_head_backImage"];
    [self addSubview:self.backImageView];
    
    self.userHeadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, YBKAdaptiveH(23), 60, 60)];
    self.userHeadImageView.layer.cornerRadius = 30;
    self.userHeadImageView.image = [UIImage imageNamed:@"header"];
    self.userHeadImageView.layer.masksToBounds = YES;
    self.userHeadImageView.userInteractionEnabled = YES;
    self.userHeadImageView.center = CGPointMake(Screen_width/2.0, self.userHeadImageView.center.y);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userHeadClick:)];
    [self.userHeadImageView addGestureRecognizer:tap];
    [self addSubview:self.userHeadImageView];
    
    self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.userHeadImageView.frame) + 10, 200, 16)];
    self.phoneLabel.textColor = [UIColor whiteColor];
    self.phoneLabel.font = [UIFont systemFontOfSize:16];
    self.phoneLabel.textAlignment = NSTextAlignmentCenter;
    self.phoneLabel.text = @"13551254947";
    self.phoneLabel.center = CGPointMake(Screen_width/2.0, self.phoneLabel.center.y);
    [self addSubview:self.phoneLabel];
    
    self.couponsButton= [[UIButton alloc] initWithFrame:CGRectMake(Screen_width/2.0-YBKAdaptiveW(60)-50, CGRectGetMaxY(self.phoneLabel.frame), 50, 40)];
    self.couponsButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.couponsButton setTitle:@"优惠0" forState:UIControlStateNormal];
    [self.couponsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.couponsButton addTarget:self action:@selector(couponsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.couponsButton];
    
    self.integralButton = [[UIButton alloc] initWithFrame:CGRectMake(Screen_width/2.0+YBKAdaptiveW(60), CGRectGetMaxY(self.phoneLabel.frame), 50, 40)];
    self.integralButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.integralButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.integralButton setTitle:@"积分0" forState:UIControlStateNormal];
    [self.integralButton addTarget:self action:@selector(integralButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.integralButton];
}

- (void)userHeadClick:(UITapGestureRecognizer *)gesture {

    if (self.userHeadClickBlock) {
        self.userHeadClickBlock();
    }
}

- (void)couponsButtonClick:(UIButton *)sender {

    if (self.clickCouponsButtonBlock) {
        
        self.clickCouponsButtonBlock();
    }
}

- (void)integralButtonClick:(UIButton *)sender {

    if (self.clickIntegralButtonBlock) {
        
        self.clickIntegralButtonBlock();
    }
}

- (void)setUserInfo:(NSDictionary *)userInfo {

    _userInfo = userInfo;
    [self.userHeadImageView sd_setImageWithURL:[NSURL URLWithString:[userInfo objectForKey:@"path"]] placeholderImage:nil];
    self.phoneLabel.text = userInfo[@"name"];
    NSString *couponsStr = [NSString stringWithFormat:@"优惠%ld",[[userInfo objectForKey:@"favourable"] integerValue]];
    [self.couponsButton setTitle:couponsStr forState:UIControlStateNormal];
    NSString *integralStr = [NSString stringWithFormat:@"积分%ld",[[userInfo objectForKey:@"point"] integerValue]];
    [self.integralButton setTitle:integralStr forState:UIControlStateNormal];
}

@end
